package gold.digger;

import gold.utils.InputUtil;

/**
 * Created by fanzhenyu02 on 2021/12/10.
 * common problem solver template.
 */
public class LC330 {
    public long startExecuteTime = System.currentTimeMillis();


    /*
     * @param 此题目参考了别人代码
     * 不是因为复杂，而是因为经典
     * 未来需要再次复习此道题目
     * @return:
     */
    class Solution {
        public int minPatches(int[] nums, int n) {
            //累加的总和
            long total = 0;
            //需要补充的数字个数
            int count = 0;
            //访问的数组下标索引
            int index = 0;
            while (total < n) {
                if (index < nums.length && nums[index] <= total + 1) {
                    //如果数组能组成的数字范围是[1,total]，那么加上nums[index]
                    //就变成了[1,total]U[nums[index],total+nums[index]]
                    //结果就是[1,total+nums[index]]
                    total += nums[index++];
                } else {
                    //添加一个新数字，并且count加1
                    total = total + (total + 1);
                    count++;
                }
            }
            return count;
        }
    }

    public void run() {
        int[] arr = InputUtil.toIntegerArray("[1,2,3]");
        System.out.println(new Solution().toString());
    }

    public static void main(String[] args) throws Exception {
        LC330 an = new LC330();
        an.run();

        System.out.println("\ncurrent solution total execute time: " + (System.currentTimeMillis() - an.startExecuteTime) + " ms.");
    }
}
